<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="招聘标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入招聘标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="招聘进度" prop="progress">
        <el-select v-model="queryParams.progress" placeholder="请选择招聘进度" clearable>
          <el-option
            v-for="dict in dict.type.sys_recruit_progress"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:recruit:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>


    <el-row :gutter="20">
      <el-col :xs="24" :sm="24" :md="24" :lg="24" v-for="item in recruitList" :key="item.id">
        <el-card class="box-card" style="margin-bottom: 20px;">
          <div class="recruit-content">
            <span>{{ item.title }}</span>
            <span style="margin-left: 20px;">
              <dict-tag :options="dict.type.sys_recruit_progress" :value="item.progress"/>
            </span>
            <span class="info" style="margin-left: 20px;">
              <span class="create-time">{{ item.createTime }}</span>
            </span>
            <span>
              <el-button type="text" @click="showContext(item)">详情</el-button>
              <el-button type="text" @click="handleDelivery(item)">投递</el-button>
              <el-button type="text" @click="handleUpdate(item)" v-hasRole="['school']">修改</el-button>
              <el-button type="text" @click="handleDelete(item)" v-hasRole="['school']">删除</el-button>
              <el-button type="text" @click="handleToCV(item)" v-hasRole="['school']">简历筛选</el-button>
            </span>

          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog :visible.sync="contextDialogVisible">
      <div v-html="currentItemContext"></div>
    </el-dialog>



    <!-- 添加或修改招聘信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="招聘标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入招聘标题" />
        </el-form-item>
        <el-form-item label="招聘内容">
          <editor v-model="form.context" :min-height="192"/>
        </el-form-item>
        <el-form-item label="招聘进度" prop="progress">
          <el-select v-model="form.progress" placeholder="请选择招聘进度">
            <el-option
              v-for="dict in dict.type.sys_recruit_progress"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecruit, getRecruit, delRecruit, addRecruit, updateRecruit } from "@/api/system/recruit";

export default {
  name: "Recruit",
  dicts: ['sys_recruit_progress'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 招聘信息表格数据
      recruitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        context: null,
        progress: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      currentItemContext: '', // 存储当前项的内容
      contextDialogVisible: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询招聘信息列表 */
    getList() {
      this.loading = true;
      listRecruit(this.queryParams).then(response => {
        this.recruitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        context: null,
        progress: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加招聘信息";
    },
    /** 修改按钮操作 */
    handleUpdate(item) {
      this.reset();
      const id = item.id || this.ids
      getRecruit(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改招聘信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecruit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecruit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除招聘信息编号为"' + ids + '"的数据项？').then(function() {
        return delRecruit(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/recruit/export', {
        ...this.queryParams
      }, `recruit_${new Date().getTime()}.xlsx`)
    },
    /** 点击显示具体招聘内容 */
    showContext(item) {
      this.currentItemContext = item.context; // 设置当前项的内容
      this.contextDialogVisible = true; // 打开对话框
    },
    /** 点击跳转简历页面 */
    handleToCV(item){
      this.$router.push({
        path: 'cv',
        query: {
          id: item.id
        }
    })
    },
    handleDelivery(item){
      this.$router.push({
        path: 'addRecruit',
        query: {
          id: item.id
        }
    })
  }
  }
};
</script>


<style>
.recruit-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.title {
  flex: 1;
}

.info {
  display: flex;
  align-items: center;
}

.create-time {
  margin-left: 20px;
}
</style>